package com.kong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kong.constant.MessageConstant;
import com.kong.dao.MenuDao;
import com.kong.entity.PageResult;
import com.kong.entity.QueryPageBean;
import com.kong.pojo.Menu;
import com.kong.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author yang2013
 * @data 2020/10/19 13:31
 */
@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    private static Integer PARENT_MENU_ID=0;
    @Autowired
    private MenuDao menuDao;

    /**
     * ----根据前端数据类型返回--
     * 前端层叠效果需要使用递归
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Menu> menus=menuDao.findByPage(queryPageBean);
        PageInfo pageInfo=new PageInfo<>(menus);
        PageResult pageResult=new PageResult(pageInfo.getTotal(),menus);
        return pageResult;
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public void addMenu(Menu menu) {
        Menu addMenu=menuDao.findById(menu.getId());
        if (addMenu.getName().equals(menu.getName())){
            throw new RuntimeException(MessageConstant.MENU_EXISTS);
        }
        menuDao.addMenu(menu);
    }

    /**
     * 1-判断是否有子菜单--有子菜单将子菜单的parentmenuId置空
     * 2-判断role——menu中间表是依赖此menu
     * @param id
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void deleteById(Integer id) {
        int count=menuDao.findParentMenuById(id);
        //如果存在子菜单则将子菜单的父菜单id置空----不允许删除
        if (count>0){
//            menuDao.updateParentMenuIdToNull(id);
            throw new RuntimeException(MessageConstant.DELETE_MENU_FAIL1);
        }
        int roleCount=menuDao.findRoleById(id);
        if (roleCount>0){
            throw new RuntimeException(MessageConstant.DELETE_MENU_FAIL1);
        }
        int effectNum=menuDao.deleteById(id);
        if (effectNum<0){
            throw new RuntimeException(MessageConstant.DELETE_MENU_FAIL);
        }
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void updateMenu(Menu menu) {
        menuDao.update(menu);
    }

    @Override
    public List<Menu> dynamicMenu(String username) {
        Integer uid=menuDao.findUidByUsername(username);
        //通过用户id查询roleid
        Set<Integer> roles=menuDao.findRoleByUid(uid);
        //判断用户是否有角色
        if (roles==null || roles.size()==0){
            throw new RuntimeException();
        }
        //查询用户所有的菜单id
        Set<Integer> allMenuId=menuDao.findMenus(roles);
        //通过rid查询所有的菜单

        //查询出用户的所有的菜单
        List<Menu> menus=menuDao.findAllMenu(allMenuId);

        List<Menu> collect = menus.stream()
                .filter(menu -> menu.getParentMenuId().equals(PARENT_MENU_ID))
                .collect(Collectors.toList());
        //通过一级菜单查询子菜单---递归
        findAllChildren(collect,menus);
        return collect;
    }

    /**
     * 递归查询所有的子菜单
     * @param collect
     * @param menus
     */
    private void findAllChildren(List<Menu> collect,List<Menu> menus) {
        for (Menu menu : collect) {
            List<Menu> menuList=new ArrayList<>();
            for (Menu menu1 : menus) {
                if (menu1.getParentMenuId().equals(menu.getId())){
                    menuList.add(menu1);
                }
                menu.setChildren(menuList);
                findAllChildren(menuList,menus);
            }
        }
    }
}
